package com.asia5b.a5mall.member.activity

import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.asia5b.a5bmall.resource.ActCons
import com.asia5b.a5mall.member.adapter.MessageAdapter
import com.asia5b.a5mall.member.databinding.ActivityMessageBinding
import com.asia5b.a5mall.middleware.base.SimpleActivity
import com.asia5b.a5mall.middleware.logic.viewmodel.member.MessageActivityVM
import com.asia5b.a5mall.middleware.model.base.ResultModel
import com.asia5b.a5mall.middleware.model.member.MessageModel
import com.asia5b.a5mall.middleware.utils.newEmptyView
import com.asia5b.a5mall.middleware.widget.refresh.AutoRefreshLayout
import com.scwang.smartrefresh.layout.api.RefreshLayout
import kotlinx.android.synthetic.main.activity_message.*

/**
 * Created by zhangyuncai on 2019/12/23.
 * 我的消息
 */
@Route(path = ActCons.MessageActivity)
class MessageActivity : SimpleActivity<MessageActivityVM, ActivityMessageBinding>() {
    override val viewModelClazz: Class<MessageActivityVM>
        get() = MessageActivityVM::class.java

    override fun getDataBinding(): ActivityMessageBinding {
        return ActivityMessageBinding.inflate(layoutInflater)
    }

    override var sIsDark = true

    var type: Int = 0

    val mDatas = arrayListOf<MessageModel>()
    val mAdapter by lazy { MessageAdapter(mDatas) }
    val mRecyclerView by lazy { rv_message }

    val emptyView by lazy { newEmptyView(mActivity) }

    override fun initView() {
        mSmartRefreshLayout=mDataBinding.autoMessage
        mRecyclerView.run {
            layoutManager = LinearLayoutManager(mActivity)
            adapter = mAdapter
        }

        getDatas(0)
    }

    override fun initEvent() {
    }

 override   fun startObserver()
    {
        mViewModel.getDatasResultLiveData.observe(this, Observer {
            getDatasResult(it)
        })
    }

    override fun initData() {

    }

    fun getDatas(refresh: Int) {
        mViewModel.getDatas(refresh)

    }

    fun getDatasResult(resultModel:ResultModel<ArrayList<MessageModel>>) {
        if (emptyView.parent == null) {
            mAdapter.setEmptyView(emptyView)
        }
        if (resultModel.page == 1) {
            mDatas.clear()
        }
        if (!resultModel.data.isNullOrEmpty()) {
            mAdapter.addData(resultModel.data!!)
        }
        mDataBinding.autoMessage.setEnableLoadMore(resultModel.page < resultModel.sumPage)
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        getDatas(1)
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        getDatas(2)
    }
}